package com.example.sen.jxdemo;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.jxccp.im.chat.common.entity.JXTraderConfig;
import com.jxccp.im.chat.manager.JXImManager;
import com.jxccp.ui.JXConstants;
import com.jxccp.ui.JXUiHelper;
import com.jxccp.ui.utils.JXContextWrapper;
import com.jxccp.ui.utils.JXPermissionUtil;
import com.jxccp.ui.view.JXConversationActivity;
import com.jxccp.ui.view.JXInitActivity;

import java.util.Locale;

/**
 * 子组织结构
 */
public class SuborgActivity extends Activity implements JXPermissionUtil
        .OnPermissionCallback, View.OnClickListener {

    JXPermissionUtil mJXPermissionUtil;
    int permissRequestCode = 1;
    Button requestBtn;
    EditText suborgEditText;
    boolean requestOrg = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_suborg);

        requestBtn = (Button) findViewById(R.id.btn_request);
        requestBtn.setOnClickListener(this);
        findViewById(R.id.btn_request_suborg).setOnClickListener(this);
        suborgEditText = findViewById(R.id.edt_suborgid_input);
        findViewById(R.id.btn_conversations).setOnClickListener(this);

        mJXPermissionUtil = new JXPermissionUtil();
    }


    public void onClick(View view) {
        if (view.getId() == R.id.btn_request) {
            requestOrg = true;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
                mJXPermissionUtil.requestPermissions(this, permissRequestCode, permissions, this);
            } else {
                //设置主商铺信息
                JXTraderConfig jxTraderConfig = new JXTraderConfig();
                jxTraderConfig.setName("主组织商铺");
                jxTraderConfig.setPid("1000");
                jxTraderConfig.setLogoUrl("https://ss0.bdstatic" +
                        ".com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png");
                jxTraderConfig.setSuborgId("nhv4odnladjucq");
                JXImManager.Config.getInstance().setTraderConfig(jxTraderConfig);
                JXUiHelper.getInstance().setNotifyEndMsg(false);
                startActivity(new Intent(this, JXInitActivity.class));
            }
        } else if (view.getId() == R.id.btn_request_suborg) {
            requestOrg = false;
            String suborgId = suborgEditText.getText().toString();
            if (!TextUtils.isEmpty(suborgId)) {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                    String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
                    mJXPermissionUtil.requestPermissions(this, permissRequestCode, permissions,
                            this);
                } else {
                    JXTraderConfig jxTraderConfig = new JXTraderConfig();
                    jxTraderConfig.setName("子组织");
                    jxTraderConfig.setPid("1001");
                    jxTraderConfig.setLogoUrl("https://ss0.bdstatic" +
                            ".com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png");
                    jxTraderConfig.setSuborgId(suborgId);
                    JXImManager.Config.getInstance().setTraderConfig(jxTraderConfig);
                    startActivity(new Intent(this, JXInitActivity.class).putExtra(JXConstants
                            .EXTRA_SUBORG_ID_KEY, suborgId));
                }
            } else {
                Toast.makeText(getApplicationContext(), "suborg id is empty", Toast.LENGTH_SHORT)
                        .show();
            }
        } else if (view.getId() == R.id.btn_conversations) {
            startActivity(new Intent(this, JXConversationActivity.class));
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
            grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        mJXPermissionUtil.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onGranted() {
        if (requestOrg) {
            //设置主商铺信息
            JXTraderConfig jxTraderConfig = new JXTraderConfig();
            jxTraderConfig.setName("主组织商铺");
            jxTraderConfig.setPid("1000");
            jxTraderConfig.setLogoUrl("https://ss0.bdstatic" +
                    ".com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png");
            jxTraderConfig.setSuborgId("nhv4odnladjucq");
            JXImManager.Config.getInstance().setTraderConfig(jxTraderConfig);
            JXUiHelper.getInstance().setNotifyEndMsg(false);
            startActivity(new Intent(this, JXInitActivity.class));
        } else {
            String suborgId = suborgEditText.getText().toString();
            if (!TextUtils.isEmpty(suborgId)) {
                JXTraderConfig jxTraderConfig = new JXTraderConfig();
                jxTraderConfig.setName("子组织");
                jxTraderConfig.setPid("1001");
                jxTraderConfig.setLogoUrl("https://ss0.bdstatic" +
                        ".com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png");
                jxTraderConfig.setSuborgId(suborgId);
                JXImManager.Config.getInstance().setTraderConfig(jxTraderConfig);
                startActivity(new Intent(this, JXInitActivity.class).putExtra(JXConstants
                        .EXTRA_SUBORG_ID_KEY, suborgId));
            } else {
                Toast.makeText(getApplicationContext(), "suborg id is empty", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    public void onDenied() {
        Toast.makeText(getApplicationContext(), "无法获取权限，请允许权限后重试", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mJXPermissionUtil = null;
    }

    @Override
    protected void onResume() {

        super.onResume();
    }


    @Override
    protected void attachBaseContext(Context newBase) {
        Locale mLocate = JXUiHelper.getInstance().getLanguge();
        Context context = JXContextWrapper.wrap(newBase, mLocate);
        super.attachBaseContext(context);
    }

}
